﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Medianamik.Core.Caching;
using Medianamik.Core.Interfaces;
using NUnit.Framework;
using Medianamik.UI.Controls;
using Medianamik.Core;
using Medianamik.Core.Action;
using Moq;

namespace Medianamik.Tests.Unit.Medianamik.UI.Controls.URLBox
{
    [TestFixture]
    public class UrlActionHandlerTest
    {
        [SetUp]
        public void Initialize()
        {
        }

        [Test]
        [Ignore]
        public void Should_get_an_url_from_a_new_node()
        {
            var cultureName = "fr-CA";
            var urlActionHandler = new UrlActionHandler();
            var node = new NodeStub();
            var nodeProperties = new List<NodeProperty>();
            var urlProperty =
                new NodeProperty(new NodeTypeProperty(Guid.NewGuid(), "Url", 100, 1, Guid.Empty, false, true, true,
                                                      false, null, String.Empty, Guid.Empty, null, Guid.Empty), null, cultureName, node);
            var nameProperty =
                new NodeProperty(new NodeTypeProperty(Guid.NewGuid(), "Name", 100, 1, Guid.Empty, false, true, true,
                                                      false, null, String.Empty, Guid.Empty, null, Guid.Empty), null, cultureName, node);


            node.NamePropertyId = Guid.NewGuid();


            nodeProperties.Add(urlProperty);
            nodeProperties.Add(nameProperty);

            CreateExistingNodes(new List<Node> { node });
            node.DefaultCulture = new CultureInfo(cultureName);
            node.ExpectedNodeProperties = nodeProperties;

            var instance = new NodeInstance(node, cultureName);
            var actionArgs = new ActionEventArgs<NodeInstance>(instance);

            instance.Name = "Page";
            node.InternalInstances.Add(instance);
            urlActionHandler.BeforeSaveNodeInstance(actionArgs);

            Assert.That(instance.Properties["Url"].GetValue<string>(), Is.Not.Empty);
        }

        public class NodeStub : Node
        {
            public List<NodeProperty> ExpectedNodeProperties
            { get; set; }

            internal override List<NodeProperty> GetNodeProperties(int? pRevision)
            {
                return ExpectedNodeProperties;
            }

            private WebSite _webSite;
            public override WebSite WebSite
            {
                get { return _webSite; }
            }
        }

        private void CreateExistingNodes(IEnumerable<Node> expectedNodes)
        {
            var cacheProvider = new Mock<INodeCacheProvider>();

            cacheProvider.Setup(x => x.GetNodes(It.IsAny<IQueryDefinition>(), It.IsAny<bool>()))
                .Returns(expectedNodes);

            NodeManager.CacheProvider = cacheProvider.Object;
        }
    }
}